package view;

import entity.Products;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;
import services.Service;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class ModifyProductsJFrame  extends JFrame implements MouseListener {
    // 定义全局组件
    JPanel backgroundPanel, labelPanel, contentPanel, buttonPanel;
    JLabel labelName, labelSuppliper, labelInventory, labelCost,labelPrice, labelJoinDate;
    JTextField name, inventory, cost,price, joinDate;
    JComboBox suppliper;
    JButton buttonAdd;

    Service service;


    public Map<String,String> suppliperMap = null;

    // 表格对象
    JTable table;
    int selectedRow;

    ProductsManagerJPanel parentPanel;
    public ModifyProductsJFrame(Service service, ProductsManagerJPanel productsManagerJPanel, JTable table, int row) {
        this.service = service;
        this.parentPanel = productsManagerJPanel;
        this.table = table;
        this.selectedRow = row;
        suppliperMap = service.selectAllIdName(1);

        initBackgroundPanel();

        this.add(backgroundPanel);

        this.setTitle("修改产品");
        this.setSize(640, 360);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }

    // 初始化背景面板
    public void initBackgroundPanel() {
        backgroundPanel = new JPanel(new BorderLayout());

        initLabelPanel();
        initContentPanel();
        initButtonPanel();

        backgroundPanel.add(labelPanel, "North");
        backgroundPanel.add(contentPanel, "Center");
        backgroundPanel.add(buttonPanel, "South");
    }
    // 初始化label面板
    public void initLabelPanel() {

        labelPanel = new JPanel();

        JLabel title = new JLabel("产品信息");
        title.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        labelPanel.add(title);
    }

    // 初始化商品信息面板
    public void initContentPanel() {
        contentPanel = new JPanel(new GridLayout(6, 2));

        labelName = new JLabel("产品名称", JLabel.CENTER);
        labelSuppliper = new JLabel("产品供应商", JLabel.CENTER);
        labelInventory = new JLabel("产品库存", JLabel.CENTER);
        labelCost = new JLabel("产品成本", JLabel.CENTER);
        labelPrice = new JLabel("产品售价", JLabel.CENTER);
        labelJoinDate = new JLabel("入库时间", JLabel.CENTER);

        name = new JTextField((String) table.getValueAt(selectedRow, 1));
        inventory = new JTextField((String) table.getValueAt(selectedRow, 3));
        cost = new JTextField((String) table.getValueAt(selectedRow, 4));
        price = new JTextField((String) table.getValueAt(selectedRow, 5));
        joinDate = new JTextField((String) table.getValueAt(selectedRow, 6));

        // 商品种类下拉框
        suppliper = new JComboBox();

        if (suppliperMap != null) {
            int sign = 0;
            int  i=0;
            for (String s : suppliperMap.keySet()) {
                if (s.equals(table.getValueAt(selectedRow, 1))) {
                    sign = i;
                    i++;
                }
                System.out.println(s);
                suppliper.addItem(s);
            }
            // 设置所选商品种类为默认种类
            suppliper.setSelectedIndex(sign);
        }

        contentPanel.add(labelName);
        contentPanel.add(name);
        contentPanel.add(labelSuppliper);
        contentPanel.add(suppliper);
        contentPanel.add(labelInventory);
        contentPanel.add(inventory);
        contentPanel.add(labelCost);
        contentPanel.add(cost);
        contentPanel.add(labelPrice);
        contentPanel.add(price);
        contentPanel.add(labelJoinDate);
        contentPanel.add(joinDate);
    }

    // 初始化按钮面板
    public void initButtonPanel() {
        buttonPanel = new JPanel();

        buttonAdd = new JButton("保存");
        buttonAdd.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue));
        buttonAdd.setForeground(Color.white);
        buttonAdd.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        buttonAdd.addMouseListener(this);

        buttonPanel.add(buttonAdd);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == buttonAdd) {

            String nameString = name.getText().trim();
            String suppliperString = (String) suppliper.getSelectedItem();
            String inventoryString = inventory.getText().trim();
            String costString = cost.getText().trim();
            String priceString = price.getText().trim();
            String joinDateString = joinDate.getText().trim();
            if (nameString.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入产品名称");
            } else if (inventoryString.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入产品库存");
            } else if (costString.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入产品成本");
            } else if (priceString.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入产品售价");
            }else if (joinDateString.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入入库时间");
            } else {
                int result = 0;
                String idString = (String) table.getValueAt(selectedRow, 0); // 在java中是最常用的获取系统时间的方法，它返回的是1970年1月1日0点到现在经过的毫秒数;
                String suppliperId = suppliperMap.get(suppliperString);
                int inventoryInt = Integer.parseInt(inventoryString);
                double costDouble = Double.parseDouble(costString);
                double priceDouble = Double.parseDouble(priceString);
                try {
                    Date joinDateDate = (new SimpleDateFormat("yy-MM-dd")).parse(joinDateString);
                    Products products = new Products(idString, nameString, suppliperId, inventoryInt,costDouble,priceDouble,joinDateDate);
                    result = service.update(0, products);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                if (result > 0) {
                    JOptionPane.showMessageDialog(null, "修改成功");
                    this.setVisible(false);
                    parentPanel.refreshTablePanel();
                }
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
